package es

import (
	"github.com/olivere/elastic/v7"
	"jmicro/app/pkg/options"
	"jmicro/pkg/db"
	"jmicro/pkg/errors"
	"sync"
)

var (
	esClient *elastic.Client
	//searchFactory v1.SearchFactory
	once sync.Once
)

//type dataSearch struct {
//	esClient *elastic.Client
//}
//
//func (ds *dataSearch) Goods() v1.UserStore {
//	return newGoodsSearch(ds)
//}
//func newGoodsSearch

//var _ v1.SearchFactory = &dataSearch{}

func GetSearchFactoryOr(opts *options.EsOptions) (*elastic.Client, error) {
	if opts == nil {
		return nil, errors.New("failed to get es client")
	}
	var err error
	once.Do(func() {
		esOpt := db.EsOptions{
			Host: opts.Host,
			Port: opts.Port,
		}
		esClient, err = db.NewEsClient(&esOpt)
		if err != nil {
			return
		}
		//searchFactory = &dataSearch{esClient: esClient}
	})
	//if searchFactory == nil {
	//	return nil, errors.New("failed to get ES client")
	//}
	return esClient, nil
}
